package com.h3c.ptability.config;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.ext.DestinationDataProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;

@Service
public class RfcManager {

    @Value("${sap.host}")
    private String host;
    @Value("${sap.clientName}")
    private String clientName;
    @Value("${sap.language}")
    private String language;
    @Value("${sap.userId}")
    private String userId;
    @Value("${sap.password}")
    private String sapPasswd;
    @Value("${sap.sysnr}")
    private String sysnr;

    private static final String ABAP_AS = "ABAP_AS_WITHOUT_POOL";
    private JCoDestination destination;

    public Properties constructSapParam() {
        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, host);
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, sysnr);
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, clientName);
        connectProperties.setProperty(DestinationDataProvider.JCO_USER, userId);
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, sapPasswd);
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG, language);
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "20");
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "30");
        return connectProperties;
    }

    public void connect() throws Exception {
        Properties connectProperties = constructSapParam();
        try {
            createDataFile(ABAP_AS, "jcoDestination", connectProperties);
            destination = JCoDestinationManager.getDestination(ABAP_AS);
        } catch (JCoException ex) {
            throw new Exception("SAP连接失败" + ex.getMessage());
        }
    }

    public JCoDestination getDestination() {
        try {
            destination = JCoDestinationManager.getDestination(ABAP_AS);
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return destination;
    }

    public static void createDataFile(String name, String suffix, Properties properties)
            throws Exception {
        File cfg = new File(name + "." + suffix);
        try {
            FileOutputStream fos = new FileOutputStream(cfg, false);
            properties.store(fos, "ABAP_AS_WITHOUT_POOL");
            fos.close();
        } catch (Exception e) {
            throw new Exception("不能创建SAP连接需要的Destination文件" + cfg.getName());
        }
    }

    public JCoFunction getFunction(JCoDestination destination, String functionName) {
        JCoFunction function = null;
        try {
            function = destination.getRepository().getFunctionTemplate(functionName).getFunction();
        } catch (JCoException e) {
            e.printStackTrace();
        }
        return function;
    }

}
